package me.flyray.crm.core.feignserver;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import javax.validation.Valid;

import com.github.wxiaoqi.security.auth.common.util.SnowFlake;
import com.github.wxiaoqi.security.biz.common.crm.request.OutAccountRequest;
import com.github.wxiaoqi.security.biz.common.crm.request.UnFreezeAndOutAccountRequest;
import com.github.wxiaoqi.security.biz.common.crm.request.UnFreezeRequest;
import com.github.wxiaoqi.security.biz.common.enums.InOutFlagEnums;
import com.github.wxiaoqi.security.common.exception.BusinessException;
import com.github.wxiaoqi.security.common.msg.BaseApiResponse;
import com.github.wxiaoqi.security.common.util.EntityUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import me.flyray.crm.core.biz.CommonOutAccountBiz;
import me.flyray.crm.core.biz.CommonOutAccountBizTcc;
import me.flyray.crm.core.entity.MerchantAccountJournal;
import me.flyray.crm.core.entity.PersonalAccountJournal;
import me.flyray.crm.core.entity.UnfreezeJournal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/***
 * 出账相关的接口
 * */
@Api(tags="出账相关的Tcc接口")
@Controller
@RequestMapping("feign")
public class FeignCustomerOutAccountTcc {
	private static final Logger log = LoggerFactory.getLogger(FeignCustomerFreezeUnFreezeTcc.class);
	
	@Autowired
	private CommonOutAccountBizTcc outAccountBizTcc;
	
	
	/***
	 * 个人或者商户解冻并出账的接口
	 * */
	@ApiOperation("个人或者商户解冻并出账的接口")
	@RequestMapping(value = "/unFreAndOutAccountTcc",method = RequestMethod.POST)
    @ResponseBody
    public BaseApiResponse unFreAndOutAccountTcc(@RequestBody @Valid UnFreezeAndOutAccountRequest unFreAndOutAccountRequest){
		log.info("FeignCustomerOutAccountTcc解冻并出账接口开始--请求参数{}"+EntityUtils.beanToMap(unFreAndOutAccountRequest));
		BaseApiResponse response = new BaseApiResponse();
		response = outAccountBizTcc.unFreAndOutAccount(unFreAndOutAccountRequest);
		log.info("FeignCustomerOutAccountTcc解冻并出账接口结束--结果{}"+response);
		return response;
	}
	
	/**
	 * 个人或者商户直接出账的接口
	 * */
	@ApiOperation("个人或者商户直接出账的接口")
	@RequestMapping(value = "/outAccountTcc",method = RequestMethod.POST)
    @ResponseBody
    public BaseApiResponse outAccountTcc(@RequestBody @Valid OutAccountRequest outAccountRequest){
		log.info("FeignCustomerOutAccountTcc直接出账接口开始--请求参数{}"+EntityUtils.beanToMap(outAccountRequest));
		BaseApiResponse response = new BaseApiResponse();
		response = outAccountBizTcc.outAccountTcc(outAccountRequest);
		log.info("FeignCustomerOutAccountTcc直接出账接口开始--请求参数{}"+EntityUtils.beanToMap(response));
		return response;
    }
	
	/**
	 * 校验冻结流水号是否被解冻
	 * @param unFreezeRequest
	 * @return
	 */
	@ApiOperation("校验冻结流水是否被解冻")
	@RequestMapping(value = "/verificationFreeze",method = RequestMethod.POST)
    @ResponseBody
    public BaseApiResponse verificationFreezeId(@RequestBody @Valid UnFreezeAndOutAccountRequest unFreAndOutAccountRequest){
		BaseApiResponse response=new BaseApiResponse();
		log.info("FeignCustomerFreezeUnFreezeTcc校验冻结流水是否被解冻--请求参数{}"+EntityUtils.beanToMap(unFreAndOutAccountRequest));
		response=outAccountBizTcc.verificationFreezeId(unFreAndOutAccountRequest);
		log.info("FeignCustomerFreezeUnFreezeTcc校验冻结流水是否被解冻--结果{}"+response);
		return response;
	}
	
	/***
	 * 个人或者商户解冻流水
	 * */
	@ApiOperation("个人或者商户解冻流水接口")
	@RequestMapping(value = "/unFreAndOutJournalTcc",method = RequestMethod.POST)
    @ResponseBody
    public BaseApiResponse unFreAndOutJournalTcc(@RequestBody @Valid UnFreezeAndOutAccountRequest unFreAndOutAccountRequest){
		BaseApiResponse response=new BaseApiResponse();
		log.info("unFreAndOutJournalTcc解冻流水接口开始--请求参数{}"+EntityUtils.beanToMap(unFreAndOutAccountRequest));		
		UnfreezeJournal queryUnfreezeJournal = new UnfreezeJournal();
		queryUnfreezeJournal.setFreezeId(unFreAndOutAccountRequest.getFreezeId());
		queryUnfreezeJournal.setJournalId(String.valueOf(SnowFlake.getId()));
		queryUnfreezeJournal.setOrderNo(unFreAndOutAccountRequest.getOrderNo());
		queryUnfreezeJournal.setTradeType(unFreAndOutAccountRequest.getTradeType());//交易类型// 01：支付，02：退款，03：提现，04：充值，05：预收款，06：消费，07：结算，08：领取
		queryUnfreezeJournal.setUnfreezeBalance(new BigDecimal(unFreAndOutAccountRequest.getUnFreezeAmt()));
		queryUnfreezeJournal.setCreateTime(new Date());
		response=outAccountBizTcc.insertUnfreezeJournal(queryUnfreezeJournal);
		log.info("unFreAndOutJournalTcc解冻流水接口结束--结果{}"+response);
		return response;
	}
	
	/***
	 * 记录商户账户流水
	 * */
	@ApiOperation("记录商户账户流水接口")
	@RequestMapping(value = "/insertMerActJournalTcc",method = RequestMethod.POST)
    @ResponseBody
    public BaseApiResponse insertMerActJournalTcc(@RequestBody @Valid UnFreezeAndOutAccountRequest unFreAndOutAccountRequest){
		BaseApiResponse response=new BaseApiResponse();
		log.info("unFreAndOutJournalTcc解冻流水接口开始--请求参数{}"+EntityUtils.beanToMap(unFreAndOutAccountRequest));		
		MerchantAccountJournal merAccountJournal=new MerchantAccountJournal();
		merAccountJournal.setJournalId(String.valueOf(SnowFlake.getId()));
		merAccountJournal.setPlatformId(unFreAndOutAccountRequest.getPlatformId());
		merAccountJournal.setMerchantId(unFreAndOutAccountRequest.getMerchantId());
		merAccountJournal.setAccountId(unFreAndOutAccountRequest.getAccountId());
		merAccountJournal.setOrderNo(unFreAndOutAccountRequest.getOrderNo());
		merAccountJournal.setInOutFlag(InOutFlagEnums.out.getCode());//出账 
		merAccountJournal.setTradeAmt(new BigDecimal(unFreAndOutAccountRequest.getUnFreezeAmt()));
		merAccountJournal.setTradeType(unFreAndOutAccountRequest.getTradeType());
		merAccountJournal.setCreateTime(new Date());
		response=outAccountBizTcc.insertMerActJournal(merAccountJournal);
		log.info("unFreAndOutJournalTcc解冻流水接口结束--结果{}"+response);
		return response;
	}
	
	/***
	 * 记录个人账户流水
	 * */
	@ApiOperation("记录个人账户流水接口")
	@RequestMapping(value = "/insertPerActJournalTcc",method = RequestMethod.POST)
    @ResponseBody
    public BaseApiResponse insertPerActJournalTcc(@RequestBody @Valid UnFreezeAndOutAccountRequest unFreAndOutAccountRequest){
		BaseApiResponse response=new BaseApiResponse();
		log.info("insertPerActJournalTcc解冻流水接口开始--请求参数{}"+EntityUtils.beanToMap(unFreAndOutAccountRequest));
		PersonalAccountJournal personalAccountJournal=new PersonalAccountJournal();
		personalAccountJournal.setJournalId(String.valueOf(SnowFlake.getId()));
		personalAccountJournal.setPlatformId(unFreAndOutAccountRequest.getPlatformId());
		personalAccountJournal.setPersonalId(unFreAndOutAccountRequest.getPersonalId());
		personalAccountJournal.setAccountId(unFreAndOutAccountRequest.getAccountId());
		personalAccountJournal.setAccountType(unFreAndOutAccountRequest.getAccountType());
		personalAccountJournal.setOrderNo(unFreAndOutAccountRequest.getOrderNo());
		personalAccountJournal.setInOutFlag(InOutFlagEnums.out.getCode());//来往标志  1：入账   2：出账
		personalAccountJournal.setTradeAmt(new BigDecimal(unFreAndOutAccountRequest.getUnFreezeAmt()));
		personalAccountJournal.setTradeType(unFreAndOutAccountRequest.getTradeType());//交易类型// 01：支付，02：退款，03：提现，04：充值，05：预收款，06：消费，07：结算，08：领取
		personalAccountJournal.setCreateTime(new Date());
		response=outAccountBizTcc.insertPerActJournal(personalAccountJournal);
		log.info("insertPerActJournalTcc解冻流水接口结束--结果{}"+response);
		return response;
	}
}
